﻿using eProcurement.BLL;
using eProcurement.controls;
using eProcurement.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eProcurement.members
{
    public partial class Announcements : System.Web.UI.Page
    {
        Manager manager = new Manager();

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                Project project = manager.GetProject( Convert.ToInt32( Request[ "pid" ] ) );
                if ( project != null )
                {
                    Hydrate( project );
                }
            }
        }

        private void Hydrate( Project project )
        {
            lblCompanyName.InnerText = project.Purchaser.CompanyName;
            lblAddress1.InnerText = project.Purchaser.Address1;
            lblAddress2.InnerText = project.Purchaser.Address2;
            lblCity.InnerText = project.Purchaser.City;
            lblWebsite.InnerHtml = "<a href='" + project.Purchaser.Website + "'>" + project.Purchaser.Website + "</a>";
            lblDescription.InnerText = project.Purchaser.Description;

            lblProjectName.InnerText = project.Title;
            lblProjectDescription.InnerText = project.Description;
            lblIndustryCategory.InnerText = project.IndustryCategory;
            lblClosureDate.InnerText = project.CloseDate.ToString( "ddd d MMM yyyy" );
            lblPostDate.InnerText = project.DatePosted.ToString( "ddd d MMM yyyy" );

            SetupAnnouncement( project.Announcements.ToList() );
        }

        private void SetupAnnouncement( List<Announcement> announcements )
        {
            foreach ( Announcement announcement in announcements )
            {
                AnnouncementItemControl control = (AnnouncementItemControl)LoadControl( "../controls/AnnouncementItemControl.ascx" );
                control.Setup( announcement );
                container.Controls.Add( control );
            }
        }
    }
}